<?php

namespace app\common\cachemodel\ai_chat_room;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class AiChatLastSendTimeRecord extends BaseCache
{
    protected $table = 'ai_chat_room.ai_chat_last_send_time_record';

    protected $cacheModel = 'ai_chat_room.chat_last_send_time_record';

    /**
     * 写入数据
     * @param $uid
     * @param $data
     * @return int
     * @throws Exception
     */
    public function SaveData($uid, $data)
    {
        try
        {
            if (isset($data['extend']) && is_array($data['extend']))
            {
                $data['extend'] = json_encode($data['extend'], JSON_UNESCAPED_UNICODE);
            }
            $id = $this->allowField(true)->insertGetId($data);
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->rmCache();
        return (int)$id;
    }

    /**
     * 更新 会话 最后发送时间 信息
     * @param int $id
     * @param int $uid
     * @param array $updateData
     * @return bool
     */
    public function UpdateData(int $id, int $uid, $updateData = [])
    {
        try
        {
            if (!$id)
            {
                $data = $this->getDataInfo($uid);
                $id = $data['id'];
            }

            $this->where(['id' => $id])->update($updateData);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setCacheTime(10)->setCache($updateData);

            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        // 获取未删除 的数据
        try
        {
            $data = $this->where(['uid' => ['in', $ids]])->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }


    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'uid', 'status', 'chat_id', 'last_send_time']);

            if (isset($itemData['extend']))
            {
                $data[$index]['extend'] = $itemData['extend'] ? json_decode($itemData['extend'], true) : [];
            }
        }

        return $data;
    }

    public function getDataInfo(int $uid)
    {
        $info = $this->getInfoById($uid);

        if (!$info)
        {   // 初始化信息
            $data = [
                'uid' => $uid,
                'status' => 1,
                'chat_id' => 0,
                'last_send_time' => 0,
                'extend' => [],
            ];

            $id = $this->SaveData($uid, $data);
            $data['id'] = $id;
            $info = $data;
        }

        return $info;
    }
}